package com.example.visitedsys.Controller;

import com.example.visitedsys.Shrio.Bean.ResponseBean;
import com.example.visitedsys.informationGet.PageVO;
import com.example.visitedsys.informationGet.PartVO;
import com.example.visitedsys.service.PartService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author  SongPeng
 * @date  2021/11/09 14:21
 * @version 1.0
 */

@RestController
@RequestMapping("/part")
public class PartController {
    @ModelAttribute
    public void doString(){
        MDC.put("operateModuleName","部门管理模块");
    }

    @Autowired
    private PartService partService;

    /**
     * 部门列表  ResponseBean
     * @return
     */
    @RequiresPermissions({"part:select"})
    @GetMapping("/findPartList")
    public ResponseBean findDepartmentList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize") Integer pageSize,
                                           PartVO partVO) {
        PageVO<PartVO> departmentsList = partService.findpartList(pageNum, pageSize, partVO);
        return ResponseBean.success(departmentsList);
    }
    /**
     * 所有部门
     *
     * @return
     */
    @GetMapping("/findAll")
    public ResponseBean findAll() {
        List<PartVO> departmentVOS = partService.findAllVO();
        return ResponseBean.success(departmentVOS);
    }
    /**
     * 添加部门
     *
     * @return
     */
    @RequiresPermissions({"part:add"})
    @PostMapping("/add")
    //@Validated进行参数验证
    public ResponseBean add(@RequestBody @Validated PartVO partVO) {
        partService.add(partVO);
        return ResponseBean.success();
    }

    /**
     * 编辑部门
     *
     * @param partId
     * @return
     */
    @RequiresPermissions({"part:update"})
    @GetMapping("/edit/{partId}")
    public ResponseBean edit(@PathVariable Long partId) {
        PartVO departmentVO = partService.edit(partId);
        return ResponseBean.success(departmentVO);
    }

    /**
     * 更新部门
     *
     * @return
     */
    @RequiresPermissions({"part:update"})
    @PutMapping("/update/{partId}")
    public ResponseBean update(@PathVariable Long partId, @RequestBody @Validated PartVO partVO) {
        partService.update(partId, partVO);
        return ResponseBean.success();
    }

    /**
     * 删除部门
     *
     * @param partId
     * @return
     */
    @ApiOperation(value = "删除部门")
    @RequiresPermissions({"part:delete"})
    @DeleteMapping("/delete/{partId}")
    public ResponseBean delete(@PathVariable Long partId) {
        partService.delete(partId);
        return ResponseBean.success();
    }


}